/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036.kiss.Entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 *
 * @author Vasa
 */
@Entity
@Table(name="ZAMESTNANEC")
public class Zamestnanec implements Cloneable, Serializable{
    
    private Long id;
    private Kolej kolej;
    private String jmeno;
    private String prijmeni;
    private String pozice;
    private String telefon;
    private String ulice;
    private String mesto;
    private String psc;
    private String username;
    private String heslo;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="ID_UCTU")
    public Long getId () {
        return this.id;
      }

      public Zamestnanec setId ( final Long id ) {

        if ( id != null ) {

          this.id = new Long ( id.longValue () );
        }

        return this;
      }

        @Column(name="JMENO")
        public String getJmeno() {
            return jmeno;
        }

        public void setJmeno(String jmeno) {
            this.jmeno = jmeno;
        }

        @Column(name="PRIJMENI")
        public String getPrijmeni() {
            return prijmeni;
        }

        public void setPrijmeni(String prijmeni) {
            this.prijmeni = prijmeni;
        }

        @Column(name="POZICE")
        public String getPozice() {
            return pozice;
        }

        public void setPozice(String pozice) {
            this.pozice = pozice;
        }

        @Column(name="TELEFON")
        public String getTelefon() {
            return telefon;
        }

        public void setTelefon(String telefon) {
            this.telefon = telefon;
        }

        @Column(name="ULICE")
        public String getUlice() {
            return ulice;
        }

        public void setUlice(String ulice) {
            this.ulice = ulice;
        }

        @Column(name="MESTO")
        public String getMesto() {
            return mesto;
        }

        public void setMesto(String mesto) {
            this.mesto = mesto;
        }

        @Column(name="PSC")
        public String getPsc() {
            return psc;
        }

        public void setPsc(String psc) {
            this.psc = psc;
        }

        @ManyToOne(fetch=FetchType.EAGER)
        @JoinColumn(name="ID_KOLEJ")
        public Kolej getKolej() {
            return kolej;
        }

        public void setKolej(Kolej kolej) {
            this.kolej = kolej;
        }

        @Column(name="HESLO")
        public String getHeslo() {
            return heslo;
        }

        public void setHeslo(String heslo) {
            this.heslo = heslo;
        }

        @Column(name="USERNAME")
        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

  @Override
  public boolean equals ( final Object rhs ) {

    if ( this == rhs ) return true;

    if ( rhs == null ) return false;

    if ( this.getClass () != rhs.getClass () ) return false;

    final Zamestnanec c = (Zamestnanec) rhs;

    return this.id == null ? c.id == null : this.id.equals ( c.id ) &&
        this.kolej == null ? c.kolej == null : this.kolej.equals ( c.kolej ) &&
        this.jmeno == null ? c.jmeno == null : this.jmeno.equals ( c.jmeno ) &&
        this.prijmeni == null ? c.prijmeni == null : this.prijmeni.equals ( c.prijmeni ) &&
        this.pozice == null ? c.pozice == null : this.pozice.equals ( c.pozice ) &&
        this.telefon == null ? c.telefon == null : this.telefon.equals ( c.telefon ) &&
        this.ulice == null ? c.ulice == null : this.ulice.equals ( c.ulice ) &&
        this.mesto == null ? c.mesto == null : this.mesto.equals ( c.mesto ) &&
        this.psc == null ? c.psc == null : this.psc.equals ( c.psc ) &&
        this.username == null ? c.username == null : this.username.equals ( c.username ) &&
        this.heslo == null ? c.heslo == null : this.heslo.equals ( c.heslo );
  }

  @Override
  public int hashCode () {

    final int[] hashCodes = {
        (this.id == null) ? 0 : this.id.hashCode (),
        (this.kolej == null) ? 0 : this.kolej.hashCode (),
        (this.jmeno == null) ? 0 : this.jmeno.hashCode (),
        (this.prijmeni == null) ? 0 : this.prijmeni.hashCode (),
        (this.pozice == null) ? 0 : this.pozice.hashCode (),
        (this.telefon == null) ? 0 : this.telefon.hashCode (),
        (this.ulice == null) ? 0 : this.ulice.hashCode (),
        (this.mesto == null) ? 0 : this.mesto.hashCode (),
        (this.psc == null) ? 0 : this.psc.hashCode (),
        (this.username == null) ? 0 : this.username.hashCode (),
        (this.heslo == null) ? 0 : this.heslo.hashCode ()
    };

    int hash = 17;
    for ( final int currentHashCode: hashCodes ) {

      hash = hash * 31 + currentHashCode;
    }

    return hash;
  }

  @Override
  public Object clone () {

    try {

      return ( (Zamestnanec) super.clone () ).setId ( this.id );
    } catch ( CloneNotSupportedException ex ) {

      assert false: "CloneNotSupportedException thrown";
      return null;
    }
  }

  @Override
  public String toString () {

    return this.id + ": " + this.jmeno + " "  + this.prijmeni+ ", "  + this.ulice
        + ", " + this.mesto + ", " + this.psc + ", " + this.username + ": " + this.heslo
    ;
  }

  /**
   * Determine whether the person has set necessary information
   */
  public void checkState () {

    final boolean noJmeno = this.jmeno == null || this.jmeno.isEmpty ();
    final boolean noPrijmeni = this.prijmeni == null || this.prijmeni.isEmpty ();
    final boolean noUsername = this.username == null || this.username.isEmpty ();
    final boolean noHeslo = this.heslo == null || this.heslo.isEmpty ();

    if ( noJmeno && noPrijmeni && noUsername && noHeslo )  {

      throw new IllegalStateException ( "Jmeno, prijmeni, username a heslo musi byt nastaveny" );
    }
  }
}